using DG.DOTweenEditor;
using DG.Tweening;
using UnityEditor;
using UnityEngine;

namespace Core.Extension.UIAnimation.Editor
{
    /// <summary>
    /// 编译器下播放动画
    /// </summary>
    [CustomEditor(typeof(UIAnimation), true)]
    public class UIPanelInspector : UnityEditor.Editor
    {
        SerializedObject obj;
        UIAnimation uiAnimation;
        private SerializedProperty playOnAwake;
        private SerializedProperty animType;
        private SerializedProperty animTarget;
        private SerializedProperty showScaleAnim;
        private SerializedProperty hideScaleAnim;
        private SerializedProperty showMoveAnim;
        private SerializedProperty hideMoveAnim;

        private void OnEnable()
        {
            obj = new SerializedObject(target);
        }

        public override void OnInspectorGUI()
        {
            uiAnimation = (UIAnimation) target;
            playOnAwake = obj.FindProperty("playOnAwake");
            animType = obj.FindProperty("animType");
            animTarget = obj.FindProperty("animTarget");
            EditorGUILayout.PropertyField(playOnAwake);
            EditorGUILayout.PropertyField(animType);
            EditorGUILayout.PropertyField(animTarget);
            if (GUILayout.Button("Show"))
            {
                uiAnimation.Awake();
                var tweener = uiAnimation.Show();
                tweener.ForEach(delegate(Tweener tweener1) { DOTweenEditorPreview.PrepareTweenForPreview(tweener1); });
                DOTweenEditorPreview.Start();
            }

            if (GUILayout.Button("Hide"))
            {
                uiAnimation.Awake();
                var tweener = uiAnimation.Hide();
                tweener.ForEach(delegate(Tweener tweener1) { DOTweenEditorPreview.PrepareTweenForPreview(tweener1); });
                DOTweenEditorPreview.Start();
            }

            showScaleAnim = obj.FindProperty("showScaleAnim");
            hideScaleAnim = obj.FindProperty("hideScaleAnim");
            showMoveAnim = obj.FindProperty("showMoveAnim");
            hideMoveAnim = obj.FindProperty("hideMoveAnim");
            if (uiAnimation.animType == UIAnimationType.Scale)
            {
                EditorGUILayout.PropertyField(showScaleAnim);
                EditorGUILayout.PropertyField(hideScaleAnim);
            }
            else if (uiAnimation.animType == UIAnimationType.Move)
            {
                EditorGUILayout.PropertyField(showMoveAnim);
                EditorGUILayout.PropertyField(hideMoveAnim);
            }

            obj.ApplyModifiedProperties();
        }
    }
}